package drds.common.db.xml_mapping.binding;

import drds.common.db.xml_mapping.session.MapperContext;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MapperProxyFactory<T> {

  private final Class<T> mapperInterface;
  private final Map<Method, MapperMethod> methodMapperMethodMap = new ConcurrentHashMap<>();

  public MapperProxyFactory(Class<T> mapperInterface) {
    this.mapperInterface = mapperInterface;
  }


  public T newInstance(MapperContext mapperContext) {
    final MapperProxy<T> mapperProxy = new MapperProxy<>(mapperContext, mapperInterface, methodMapperMethodMap);
    return newInstance(mapperProxy);
  }

  protected T newInstance(MapperProxy<T> mapperProxy) {
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[]{mapperInterface}, mapperProxy);
  }

}
